home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / daten / astrolog / src / rastport.c < prev    next >
C/C++ Source or Header  |  1995-08-11  |  5KB  |  268 lines

  1. /*                                                               -*- C -*-
  2.  *  RASTPORT.C
  3.  *
  4.  *  (c)Copyright 1991 by Tobias Ferber,  All Rights Reserved
  5.  */
  6.  
  7. /* $VER: $Id: rastport.c,v 1.1 1995/06/21 22:18:28 tf Exp $ */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <graphics/rastport.h>
  12. #include <graphics/gfx.h>
  13. #include <stdlib.h>
  14.  
  15. #include "rastport.h"
  16.  
  17. /* Prototypes */
  18.  
  19. extern void *AllocMem(ULONG, ULONG);
  20. extern void FreeMem(void *,ULONG);
  21.  
  22. extern void InitBitMap( struct BitMap *, BYTE, UWORD, UWORD );
  23. extern PLANEPTR AllocRaster(ULONG,ULONG);
  24. extern void FreeRaster( PLANEPTR, USHORT, USHORT);
  25. extern void InitRastPort(struct RastPort *rp);
  26. extern void SetRast( struct RastPort *, UBYTE );
  27.  
  28. /**/
  29.  
  30. typedef struct rpnode {
  31.   struct rpnode *succ, *pred;
  32.   struct RastPort *rp;
  33.   int width, height, depth; 
  34. } rpnode_t;
  35.  
  36. static rpnode_t *rplist= (rpnode_t *)0;
  37.  
  38.  
  39. /* remember bitmap dimensions for given rastport */
  40. static int rp_set(struct RastPort *rp, int width, int height, int depth)
  41. {
  42.   int err= 0;
  43.  
  44.   if(rp)
  45.   {
  46.     rpnode_t *this= (rpnode_t *)malloc( sizeof(rpnode_t) );
  47.  
  48.     if(this)
  49.     {
  50.       this->pred= (rpnode_t *)0;
  51.  
  52.       if( (this->succ= rplist) )
  53.         rplist->pred= this;
  54.  
  55.       rplist= this;
  56.  
  57.       this->rp     = rp;
  58.       this->width  = width;
  59.       this->height = height;
  60.       this->depth  = depth;
  61.     }
  62.     else err= 2;
  63.   }
  64.   else err= 1;
  65.  
  66.   return err;
  67. }
  68.  
  69.  
  70. /* recall and then forget bitmap dimensions for given rastport */
  71. static int rp_get(struct RastPort *rp, int *width, int *height, int *depth)
  72. {
  73.   int err= 0;
  74.  
  75.   if(rp)
  76.   {
  77.     rpnode_t *this;
  78.  
  79.     for(this= rplist; this; this= this->succ)
  80.       if( (long)this->rp == (long)rp )
  81.         break;
  82.  
  83.     if(this)
  84.     {
  85.       if(width)  *width  = this->width;
  86.       if(height) *height = this->height;
  87.       if(depth)  *depth  = this->depth;
  88.  
  89.       if(this->pred)
  90.         this->pred->succ= this->succ;
  91.  
  92.       else
  93.         rplist= this->succ;
  94.  
  95.       if(this->succ)
  96.         this->succ->pred= this->pred;
  97.  
  98.       free(this);
  99.     }
  100.     else err= 1; /* rp not in list */
  101.   }
  102.   else err= 2; /* no rp */
  103.  
  104.   return err;
  105. }
  106.  
  107.  
  108. /****** rastport/rp_dispose **************************************************
  109. *
  110. *   NAME
  111. *       rp_dispose -- Free a RastPort allocated via rp_new()
  112. *
  113. *   SYNOPSIS
  114. *       nil= rp_dispose(rp);
  115. *
  116. *       struct RastPort *rp_dispose(struct RastPort *);
  117. *
  118. *   FUNCTION
  119. *       This function returns all memory allocated for the given RastPort,
  120. *       the attached rp->BitMap and all bitplanes to the system free pool.
  121. *
  122. *   INPUTS
  123. *       rp            - A RastPort allocated vi rp_new()
  124. *
  125. *   RESULT
  126. *       nil           - if rp has been allocated via rp_new() then
  127. *                       (struct RastPort *)NULL will be returned,
  128. *                       rp (unchanged), otherwise.
  129. *
  130. *   EXAMPLE
  131. *
  132. *   NOTES
  133. *
  134. *   BUGS
  135. *
  136. *   SEE ALSO
  137. *       rp_new()
  138. *
  139. ******************************************************************************
  140. *
  141. *
  142. */
  143.  
  144. struct RastPort *rp_dispose(struct RastPort *rp)
  145. {
  146.   int width, height, depth;
  147.  
  148.   if( rp_get(rp, &width, &height, &depth) == 0 )
  149.   {
  150.     struct BitMap *bm= rp->BitMap;
  151.  
  152.     if(bm)
  153.     {
  154.       int p;
  155.  
  156.       for(p=0; p<depth; p++)
  157.       {
  158.         if(bm->Planes[p])
  159.           FreeRaster(bm->Planes[p],width,height);
  160.       }
  161.       FreeMem( (void *)bm, sizeof(struct BitMap) );
  162.     }
  163.     FreeMem( (void *)rp, sizeof(struct RastPort) );
  164.     rp= (struct RastPort *)0;
  165.   }
  166.  
  167.   return rp;
  168. }
  169.  
  170.  
  171. /****** rastport/rp_new ******************************************************
  172. *
  173. *   NAME
  174. *       rp_new -- Allocate a RastPort including a BitMap and Bitplanes
  175. *
  176. *   SYNOPSIS
  177. *       rp= rp_new(width, height, depth);
  178. *
  179. *       struct RastPort *rp_new(int, int, int);
  180. *
  181. *   FUNCTION
  182. *       This function allocates a new RastPort and attaches a new BitMap to
  183. *       it with initialized, empty bitplanes.
  184. *
  185. *   INPUTS
  186. *       width         -
  187. *       height        -
  188. *       depth         -
  189. *
  190. *   RESULT
  191. *       rp            - A new RastPort
  192. *
  193. *   EXAMPLE
  194. *
  195. *   NOTES
  196. *
  197. *   BUGS
  198. *
  199. *   SEE ALSO
  200. *       rp_dispose()
  201. *
  202. ******************************************************************************
  203. *
  204. *
  205. */
  206.  
  207. struct RastPort *rp_new(int width, int height, int depth)
  208. {
  209.   struct RastPort *rp= (struct RastPort *)0;
  210.  
  211.   if( (width > 0) && (height > 0) && (depth > 0) )
  212.   {
  213.     rp= (struct RastPort *)AllocMem( sizeof(struct RastPort), MEMF_PUBLIC|MEMF_CLEAR );
  214.  
  215.     if(rp)
  216.     {
  217.       struct BitMap *bm= (struct BitMap *)AllocMem( sizeof(struct BitMap), MEMF_PUBLIC|MEMF_CLEAR );
  218.  
  219.       if(bm)
  220.       {
  221.         int p;
  222.  
  223.         InitBitMap(bm, depth, width, height);
  224.  
  225.         for(p=0; p<depth; p++)
  226.         {
  227.           PLANEPTR ptr= (PLANEPTR)AllocRaster(width,height);
  228.  
  229.           if(ptr)
  230.             bm->Planes[p]= ptr;
  231.  
  232.           else
  233.             break;
  234.         }
  235.  
  236.         if(p == depth)
  237.         {
  238.           InitRastPort(rp);
  239.           rp->BitMap= bm;
  240.           SetRast(rp, 0L);
  241.  
  242.           rp_set(rp, width,height,depth);
  243.         }
  244.  
  245.         else /* AllocRaster() failed for plane `p' */
  246.         {
  247.           for(--p; p>=0; p--)
  248.           {
  249.             if(bm->Planes[p])
  250.               FreeRaster(bm->Planes[p],width,height);
  251.           }
  252.           FreeMem( (void *)bm, sizeof(struct BitMap) );
  253.           bm= (struct BitMap *)0;
  254.         }
  255.       }
  256.       else /* !bm */
  257.       {
  258.         FreeMem( (void *)rp, sizeof(struct RastPort) );
  259.         rp= (struct RastPort *)0;
  260.       }
  261.     }
  262.     else /* !rp */
  263.       ;
  264.   }
  265.  
  266.   return rp;
  267. }
  268.